package org.apollo.base.tools.ui.view;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JLabel;

/**
 * @ClassName: LinkLabel
 * @Description: 下划线label
 * @author wuzhenfang(wzfbj2008@163.com)
 * @date 2017年8月1日 下午6:12:09
 * @version V1.0
 */
public class LinkLabel extends JLabel {

	/** serialVersionUID */
	private static final long serialVersionUID = 5536368501963764796L;

	private Color defaultColor;
	private Color selectedColor = Color.ORANGE;
	private Color indicatedColor;

	private boolean isDrawLine = true;

	/** Whether show the hand cursor or not. */
	private boolean handCursor = true;

	private boolean hovered;
	
	private Vector<ActionListener> listeners = new Vector<ActionListener>();
	
	public LinkLabel() {
		super();
		defaultColor = this.getForeground();
		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent event) {// 鼠标进入
				handleMouseEntered(event);
			}

			public void mouseExited(MouseEvent event) {// 鼠标移除
				handleMouseExited(event);
			}

			public void mouseReleased(MouseEvent event) {// 鼠标释放
				handleMouseReleased(event);
			}

			public void mouseClicked(MouseEvent event) {// 鼠标点击
				handleMouseClicked(event);
			}
		});
	}

	public void addActionListener(ActionListener listener) {
		if (!listeners.contains(listener))
			listeners.add(listener);
	}

	public void removeActionListener(ActionListener listener) {
		if (listeners.contains(listener))
			listeners.remove(listener);
	}

	protected void fireActionPerformed(ActionEvent e) {
		for (ActionListener listener : listeners) {
			listener.actionPerformed(e);
		}
	}

	public void handleMouseClicked(MouseEvent event) {
		/** The actionPerformed. */
		this.fireActionPerformed(new ActionEvent(this, this.hashCode(), this.getText()));
		// DrawLine color
//		pressed = true;
	}

	public void handleMouseEntered(MouseEvent event) {
		setHovered(true);
		if (handCursor) {
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}

		this.setForeground(getSelectedColor());
		if (isDrawLine) {
			this.repaint();
		}
	}

	public void handleMouseExited(MouseEvent event) {
		setHovered(false);
		this.setForeground(defaultColor);
		if (isDrawLine) {
			this.repaint();
		}
	}

	public void handleMouseReleased(MouseEvent event) {

	}

	public void paint(Graphics g) {
		super.paint(g);
		int w = this.getWidth();
		int h = this.getHeight();
		int wd = 0;

		FontMetrics fm = g.getFontMetrics();
		if (this.getText() != null)
			wd += fm.stringWidth(this.getText());
		int x = (int) (w - wd);

		if (isHovered()) {
			int y = (h - fm.getHeight()) / 2 + fm.getAscent() + 2;
			g.setColor(defaultColor);
			g.drawLine(x, y, x + w, y);
		}
	}

	public Color getDefaultColor() {
		return defaultColor;
	}

	public Color getSelectedColor() {
		return selectedColor;
	}

	public Color getIndicatedColor() {
		return indicatedColor;
	}

	public void setDefaultColor(Color c) {
		defaultColor = c;
	}

	public void setDrawLine(boolean b) {
		isDrawLine = b;
	}

	public void setIndicatedColor(Color c) {
		indicatedColor = c;
	}

	public void setSelectedColor(Color c) {
		selectedColor = c;
	}
	
	public boolean isHovered() {
		return hovered;
	}

	public void setHovered(boolean hovered) {
		this.hovered = hovered;
	}

}
